package com.bieyang.anjia.net;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.bieyang.anjia.module.Device;
import com.bieyang.anjia.module.LoginTypeEnum;
import com.bieyang.anjia.module.User;
import com.bieyang.anjia.refresh.BasicHandler;
import com.bieyang.anjia.ui.activity.App;
import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.LogUtils;
import com.bieyang.anjia.utils.NetUtils;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.http.volley.Request;
import com.http.volley.Request.Method;
import com.http.volley.Request.Type;
import com.http.volley.Response;
import com.http.volley.Response.ErrorListener;
import com.http.volley.Response.Listener;
import com.http.volley.VolleyClient;
import com.http.volley.VolleyError;
import com.http.volley.toolbox.StringRequest;

public class NetProccess {

	static NetProccess instanse;

	VolleyClient client;

	Context context;

	private NetProccess(Context context) {
		this.context = context;
		client = new VolleyClient(this.context, new ResponseHandler());
	}

	public static NetProccess getDefault(Context context) {
		if (instanse == null)
			instanse = new NetProccess(context);
		return instanse;
	}

	/**
	 * cookie读取和发送
	 * 
	 * */
	public void login(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_LOGIN;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("loginId", user.getCardNo());
		mMap.put("password", user.getPassword());
		mMap.put("uuid", user.getUuid());
		mMap.put("type", LoginTypeEnum.MAIN.getCode());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段
		}

		try {
			client.start(jsonObjectPostRequest);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 会员注册
	 * 
	 * */
	public void register(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_REGISTER;
		StringRequest request = (StringRequest) client.createRequest(url, null);
		request.setType(Type.TYPE_JSONOBJECT);
		Map<String, String> entryMap = new HashMap<String, String>();
		entryMap.put("userName", user.getLoginId());
		entryMap.put("password", user.getPassword());
		entryMap.put("uuid", user.getUuid());
		entryMap.put("code", "946409");
		entryMap.put("type", "01");//user.getIdentify().getCode()
		
		request.setPostEntry(entryMap);
		request.setTag(NetUtils.genericResponse(
				BasicHandler.TYPE_USER_REGISTER, user, "User"));

		try {
			client.start(request);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 会员退出
	 * 
	 * */
	public void logout() {
		String url = Constants.SERVER_URL + Constants.ACTION_LOGOUT;

		HashMap<String, String> mMap = new HashMap<String, String>();

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 设备登录
	 * 
	 * */
	public void devLogin(String userName, String password, String uuid,
			String type) {
		HttpPost request = new HttpPost("");
		HttpClient httpClient = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", userName));
		params.add(new BasicNameValuePair("password", password));

		HttpResponse response;
		try {
			HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
			request.setEntity(entity);
			response = httpClient.execute(request);

			// 如果返回状态为200，获得返回的结果
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String str = EntityUtils.toString(response.getEntity());
				System.out.println("response:" + str);
				if (str.trim().equals("success")) {
					// 用户登录成功
					System.out.println("登录成功！");

				} else {
					System.out.println("登录失败！");
				}

			} else {
				System.out.println("连接失败！");

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 新增收件地址
	 * 
	 * */
	public void addAddress(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_ADD_ADDRESS;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("memberId", user.getMemberId() + "");
		mMap.put("name", user.getReciverName());
		mMap.put("phone", user.getReciverPhone());
		mMap.put("zipCode", user.getZipCode());
		mMap.put("provinceCity", user.getProvinceCity());
		mMap.put("district", user.getDistrict());
		mMap.put("address", user.getAddress());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 修改收件地址
	 * 
	 * */
	public void updateAddress(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_UPDATE_ADDRESS;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("id", user.getAddressId());
		mMap.put("name", user.getReciverName());
		mMap.put("memberId", user.getMemberId() + "");
		mMap.put("phone", user.getReciverPhone());
		mMap.put("zipCode", user.getZipCode());
		mMap.put("provinceCity", user.getProvinceCity());
		mMap.put("district", user.getDistrict());
		mMap.put("address", user.getAddress());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取所有收件地址
	 * 
	 * */
	public void selectAdderss(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_SELECT_ADDRESS;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("memberId", user.getMemberId() + "");

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 主机下的所有忠机
	 * 
	 * */
	public void allSub(Device divice) {
		

//		HashMap<String, String> mMap = new HashMap<String, String>();
//		mMap.put("memberId", divice.getMemberId());
//
//		// 发起请求
//		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
//				url, new Response.Listener<JSONObject>() {
//
//					@Override
//					public void onResponse(Request<JSONObject> arg0,
//							JSONObject response, Map<String, String> arg2) {
//
//						LogUtils.LogProtocol("NEWRES:" + response.toString());
//
//						try {
//							Log.d("login-success", response.getString("Cookie"));
//							SharedPrefsUtil.putValue(context, "cookie",
//									response.getString("cookie"));
//
//							if (response.get("status").equals("0")) {
//								// 登录成功
//								Log.d("login-success", "success");
//							}
//						} catch (JSONException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//					}
//
//				}, new Response.ErrorListener() {
//
//					@Override
//					public void onErrorResponse(Request arg0, VolleyError arg1) {
//						// TODO Auto-generated method stub
//
//					}
//
//				}, mMap);
//		String localCookieStr = SharedPrefsUtil.getStringValue(App.mContext,
//				"cookie", "");
//		Log.d("localCookieStr", localCookieStr);
//
//		if (!localCookieStr.equals("")) {
//			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段
//
//			try {
//				client.start(jsonObjectPostRequest);
//			} catch (IllegalAccessException e) {
//				e.printStackTrace();
//			}
//		}
	}

	/**
	 * 删除忠机
	 * 
	 * */
	public void deleteSub(Device divice) {
		String url = Constants.SERVER_URL + Constants.ACTION_DELETE_SUB;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("id", divice.getDiviceId());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 忠机名称修改
	 * */
	public void updateSubName(Device divice) {
		String url = Constants.SERVER_URL + Constants.ACTION_UPDATE_SUB_NAME;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("id", divice.getDiviceId());
		mMap.put("name", divice.getDiviceName());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 设备状态新增
	 * */
	public void insertDiviceStates(Device divice) {
		String url = Constants.SERVER_URL
				+ Constants.ACTION_INSERT_DIVICE_STATES;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("uuid", divice.getUuid());
		mMap.put("type", divice.getType());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 设备状态查询
	 * */
	public void queryDiviceStates(Device divice) {
		String url = Constants.SERVER_URL
				+ Constants.ACTION_QUERY_DIVICE_STATES;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("uuid", divice.getUuid());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 报警记录新增
	 * */
	public void insertAlarm(Device divice) {
		String url = Constants.SERVER_URL + Constants.ACTION_INSERT_ALARM;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("uuid", divice.getUuid());
		mMap.put("memberId", divice.getMemberId());
		mMap.put("cardNo", divice.getCardNo());
		mMap.put("type", divice.getAlarmType());
		mMap.put("alarmTime", divice.getAlarmTime());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 报警记录查询
	 * */
	public void queryAlarm(Device divice) {
		String url = Constants.SERVER_URL + Constants.ACTION_QUERY_ALARM;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("uuid", divice.getUuid());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 采集端新增、修改
	 * */
	public void updateDiviceSet(Device divice) {
		String url = Constants.SERVER_URL + Constants.ACTION_QUERY_DIVICE_SET;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("id", divice.getDiviceId());
		mMap.put("uuid", divice.getUuid());
		mMap.put("startTWhite", divice.getStartTWhite());
		mMap.put("endTWhite", divice.getEndTWhite());
		mMap.put("deterrenceVWhite", divice.getDeterrenceVWhite());
		mMap.put("larmWhite", divice.getLarmWhite());
		mMap.put("weekWhite", divice.getWeekWhite());
		mMap.put("startTNight", divice.getStartTNight());
		mMap.put("endTNight", divice.getEndTNight());
		mMap.put("deterrenceVNight", divice.getDeterrenceVNight());
		mMap.put("larmNight", divice.getLarmNight());
		mMap.put("weekNight", divice.getWeekNight());
		mMap.put("primaryWarn", divice.isPrimaryWarn() ? "T" : "F");
		mMap.put("advancedWarning", divice.isAdvancedWarning() ? "T" : "F");
		mMap.put("humanR", divice.isHumanR() ? "T" : "F");
		mMap.put("motionD", divice.isMotionD() ? "T" : "F");
		mMap.put("flashlight", divice.isFlashlight() ? "T" : "F");
		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 采集端查看
	 * */
	public void queryDiviceSet(Device divice) {
		String url = Constants.SERVER_URL + Constants.ACTION_QUERY_DIVICE_SET;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("memberId", divice.getMemberId());
		mMap.put("uuid", divice.getUuid());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 用户基本信息查询
	 * */
	public void queryUserInfo(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_QUERY_USER_INFO;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("id", user.getInfoId());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 用户基本信息修改
	 * */
	public void updateUserInfo(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_UPDATE_USER_INFO;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("id", user.getInfoId());
		mMap.put("mark", user.getMark() + "");
		switch (user.getMark()) {
		case 1:
			mMap.put("imgHead", user.getImgHead());
			break;
		case 2:
			mMap.put("nickName", user.getNickName());
			break;
		case 3:
			mMap.put("mobile", user.getMobile());
			break;
		case 4:
			mMap.put("level", user.getLevel());
			break;
		case 5:
			mMap.put("integral", user.getIntegral());
			break;
		case 6:
			mMap.put("email", user.getEmail());
			break;
		case 7:
			mMap.put("gender", user.getGender());// 01、02
			break;
		case 8:
			mMap.put("birthday", user.getBirthday());
			break;
		case 9:
			mMap.put("type", user.getUserType());
			break;
		default:
			break;
		}

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 用户密码重置
	 * */
	public void updatePassword(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_UPDATE_PASSWORD;

		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("cardNo", user.getCardNo());
		mMap.put("password", user.getPassword());
		mMap.put("pwLevel", user.getPwLevel());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 通知、是否上传、加我验证修改
	 * */
	public void updateManagement(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_UPDATE_MANAGEMENT;

		HashMap<String, String> mMap = new HashMap<String, String>();

		mMap.put("id", user.getMessageId() + "");
		mMap.put("mark", user.getMark() + "");
		switch (user.getMark()) {
		case 1:
			mMap.put("messageAlerts", user.getMessageAlerts());
			break;
		case 2:
			mMap.put("cloudUploaded", user.getCloudUploaded());
			break;
		case 3:
			mMap.put("verification", user.getVerification());
			break;

		default:
			break;
		}

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 消息是否通知查询
	 * */
	public void selectMessageAlert(User user) {
		String url = Constants.SERVER_URL
				+ Constants.ACTION_SELECT_MESSAGE_ALERT;

		HashMap<String, String> mMap = new HashMap<String, String>();

		mMap.put("id", user.getMessageId() + "");
		mMap.put("messageAlerts", user.getMessageAlerts());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 是否上传云与加我验证查询
	 * */
	public void selectBoth(User user) {
		String url = Constants.SERVER_URL + Constants.ACTION_SELECT_BOTH;

		HashMap<String, String> mMap = new HashMap<String, String>();

		mMap.put("id", user.getMessageId() + "");
		mMap.put("cloudUploaded", user.getCloudUploaded());
		mMap.put("verification", user.getVerification());

		// 发起请求
		JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(
				url, new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(Request<JSONObject> arg0,
							JSONObject response, Map<String, String> arg2) {

						LogUtils.LogProtocol("NEWRES:" + response.toString());

						try {
							Log.d("login-success", response.getString("Cookie"));
							SharedPrefsUtil.putValue(context, "Cookie",
									response.getString("Cookie"));

							if (response.get("status").equals("0")) {
								// 登录成功
								Log.d("login-success", "success");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(Request arg0, VolleyError arg1) {
						// TODO Auto-generated method stub

					}

				}, mMap);
		String localCookieStr = SharedPrefsUtil.getStringValue(context,
				"Cookie", "");

		if (!localCookieStr.equals("")) {
			jsonObjectPostRequest.setSendCookie(localCookieStr);// 向服务器发起post请求时加上cookie字段

			try {
				client.start(jsonObjectPostRequest);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 心跳
	 * 
	 * */
	public void sendAlive(Listener<String> handler, ErrorListener faildListener) {
		String url = Constants.SERVER_URL + Constants.ACTION_ALIVE;

		StringRequest request = (StringRequest) client.createRequest(Method.POST, url, handler, faildListener);
		request.setType(Type.TYPE_JSONOBJECT);
		request.setTag(NetUtils.genericResponse(BasicHandler.TYPE_ALIVE, null, null));
		try {
			client.start(request);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}
